package tom.client.admin;

import java.util.Map;

import tom.client.DataForm;
import tom.client.Library;
import tom.client.LookupDialog;
import tom.shared.PostCode;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextArea;

public class FormPostCode extends DataForm {
	public FormPostCode() {
		Label lblNewLabel = new Label("รหัสไปรษณีย์");
		add(lblNewLabel, 23, 123);

		Label lblNewLabel_1 = new Label("อำเภอ");
		add(lblNewLabel_1, 23, 166);

		txtPostCode = new TextBox();
		txtPostCode.setMaxLength(5);
		add(txtPostCode, 122, 123);

		txtAmphur = new TextBox();
		txtAmphur.setMaxLength(20);
		add(txtAmphur, 122, 166);

		txtProvince = new TextBox();
		txtProvince.setMaxLength(20);
		add(txtProvince, 122, 210);

		txtNote = new TextArea();
		txtNote.setVisibleLines(5);
		add(txtNote, 122, 252);
		txtNote.setSize("213px", "64px");

		txtPostOffice = new TextBox();
		add(txtPostOffice, 122, 339);

		Label lblNewLabel_2 = new Label("จังหวัด");
		add(lblNewLabel_2, 23, 210);

		Label lblNewLabel_3 = new Label("หมายเหตุ");
		add(lblNewLabel_3, 23, 252);

		Label lblNewLabel_4 = new Label("ที่ทำการไปรษณีย์");
		add(lblNewLabel_4, 23, 339);
		lblNewLabel_4.setSize("74px", "39px");
		this.setFieldsHandler();
	}

	private static FormPostCode panel;

	public static FormPostCode getPanel() {
		if (panel == null) {
			panel = GWT.create(FormPostCode.class);
		}
		return panel;
	}

	PostCode record;

	@Override
	protected void search() {
		LookupDialog.show("รหัสไปรษณีย์", "postCode", txtCode);
	}

	@Override
	protected void refresh() {
		Library.getAdminService().selectPostCode(
				Integer.valueOf(txtCode.getText()), dataCallback);
	}

	@Override
	protected void update() {
		getFields();
		Library.getAdminService().updatePostCode(record, delCallback);
	}

	@Override
	protected void insert() {
		getFields();
		Library.getAdminService().insertPostCode(record, delCallback);

	}

	@Override
	protected void delete() {
		Library.getAdminService().deletePostCode(
				Integer.valueOf(txtCode.getText()), delCallback);

	}

	@Override
	protected void clearFields() {
		super.clearFields();
		txtPostCode.setText(null);
		txtAmphur.setText(null);
		txtProvince.setText(null);
		txtNote.setText(null);
		txtPostOffice.setText(null);
	}

	@Override
	protected void setFields() {
		super.setFields();
		if (record != null) {
			txtCode.setText(record.id.toString());
			txtPostCode.setText(record.postCode);
			txtAmphur.setText(record.amphur);
			txtProvince.setText(record.province);
			txtNote.setText(record.note);
			txtPostOffice.setText(record.postOffice);
		} else {
			String code = txtCode.getText();
			clearFields();
			super.setMode(3);
			txtCode.setText(code);
		}
	}

	@Override
	protected void getFields() {
		if (record == null)
			record = new PostCode();
		record.id = Integer.valueOf(txtCode.getText());
		record.postCode = txtPostCode.getText();
		record.amphur = txtAmphur.getText();
		record.province = txtProvince.getText();
		record.note = txtNote.getText();
		record.postOffice = txtPostOffice.getText();

	}

	@Override
	protected String getReportFile() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected Map<String, Object> genReportParams() {
		// TODO Auto-generated method stub
		return null;
	}

	private AsyncCallback<PostCode> dataCallback = new AsyncCallback<PostCode>() {
		public void onFailure(Throwable caught) {
			Window.alert(Library.ERROR_MESSAGE);
		}

		@Override
		public void onSuccess(PostCode result) {
			record = result;
			setFields();
		}
	};
	private final TextBox txtPostCode;
	private final TextBox txtAmphur;
	private final TextBox txtProvince;
	private final TextArea txtNote;
	private final TextBox txtPostOffice;

}
